/**
 * 格式化
 * yyyy-MM-dd或yyyy/MM/dd或yyyyMMdd
 * 默认为yyyy-MM-dd
 */
function formatDate(val,reg){
	if(val==null || val == ""){
		return "";
	}
	var date = new Date(val);
	var year = date.getFullYear()+"";
	var month = date.getMonth()+1+"";
	if(month.length==1){
		month = "0"+month;
	}
	var day = date.getDate()+"";
	if(day.length==1){
		day = "0"+day;
	}
	if (typeof(reg) == "undefined"){
		return year+"-"+month+"-"+day;
	}else{
		return year+reg+month+reg+day;
	}
}

function formatMonth(val,reg){
    if(val==null){
        return "";
    }
    var date = new Date(val);
    var year = date.getFullYear()+"";
    var month = date.getMonth()+1+"";
    if(month.length==1){
        month = "0"+month;
    }
    if (typeof(reg) == "undefined"){
        return year+"-"+month;
    }else{
        return year+reg+month+reg;
    }
}

function formatYear(val){
    if(val==null){
        return "";
    }
    var date = new Date(val);
    var year = date.getFullYear()+"";

    return year;
}

/**
 * 格式化
 * yyyy-MM-dd HH:mm:ss或yyyy/mm/dd HH:mm:ss
 * 默认为yyyy-MM-dd HH:mm:ss
 */
function formatDateTime(val,reg,isCurrentTime){
	if(val==null){
		return "";
	}
	var date = new Date(val);
	var year = date.getFullYear()+"";
	var month = date.getMonth()+1+"";
	if(month.length==1){
		month = "0"+month;
	}
	var day = date.getDate()+"";
	if(day.length==1){
		day = "0"+day;
	}
	var hour = date.getHours()+"";
	if(hour.length==1){
		hour = "0"+hour;
	}
	var minute = date.getMinutes()+"";
	if(minute.length==1){
		minute = "0"+minute;
	}
	var second = date.getSeconds()+"";
	if(second.length==1){
		second = "0"+second;
	}
	if(typeof(isCurrentTime) == "undefined"&&isCurrentTime!=true){
		if (typeof(reg) == "undefined"){
			return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
		}else{
			return year+reg+month+reg+day+" "+hour+":"+minute+":"+second;
		}
	}else{
		var curDate = new Date();
		var curYear = curDate.getFullYear()+"";
		var curDay = curDate.getDate()+"";
		if(curDay.length==1){
			curDay = "0" + curDay;
		}
		if(curDay==day){
			return hour+":"+minute+":"+second;
		}else{
			if(curYear==year){
				if (typeof(reg) == "undefined"){
					return month+"-"+day;
				}else{
					return month+reg+day;
				}
			}else{
				if (typeof(reg) == "undefined"){
					return year+"-"+month+"-"+day;
				}else{
					return year+reg+month+reg+day;
				}
			}
		}
	}
}

/**
 * 获取格式化时间
 * yyyy-MM-dd HH:mm:ss或yyyy/mm/dd HH:mm:ss
 * 默认为yyyy-MM-dd HH:mm:ss
 */
function getFormatDateTime(val, reg) {
	if(typeof(val) != 'number'){
		return "";
	}
	var date = new Date(val*1000);
    var year = date.getFullYear()+"";
    var month = date.getMonth()+1+"";
    if(month.length==1){
        month = "0"+month;
    }
    var day = date.getDate()+"";
    if(day.length==1){
        day = "0"+day;
    }
    var hour = date.getHours()+"";
    if(hour.length==1){
        hour = "0"+hour;
    }
    var minute = date.getMinutes()+"";
    if(minute.length==1){
        minute = "0"+minute;
    }
    var second = date.getSeconds()+"";
    if(second.length==1){
        second = "0"+second;
    }
    if (typeof(reg) == "undefined"){
        return year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
    }else{
        return year+reg+month+reg+day+" "+hour+":"+minute+":"+second;
    }
}

/**
 * 获取结算周期时间
 * 传入一个时间戳，返回这个时间所在月份第一天和最后一天字符串
 */
function getMonthCycle(time){
    if(time==null){
        return "";
    }
    var rusult;
    var date = new Date(time);
    var year = date.getFullYear()+"";
    var month = date.getMonth()+1+"";
    if(month.length==1){
        month = "0"+month;
    }
    rusult  = year+"-"+month+"-01至"+year+"-"+month;
    switch (month){
		case "01":
            rusult = rusult+"-31";
			break;
		case "02":
            rusult = rusult+"-28";
			break;
        case "03":
            rusult = rusult+"-31";
            break;
        case "04":
            rusult = rusult+"-30";
            break;
        case "05":
            rusult = rusult+"-31";
            break;
        case "06":
            rusult = rusult+"-30";
            break;
        case "07":
            rusult = rusult+"-31";
            break;
        case "08":
            rusult = rusult+"-31";
            break;
        case "09":
            rusult = rusult+"-30";
            break;
        case "10":
            rusult = rusult+"-31";
            break;
        case "11":
            rusult = rusult+"-30";
            break;
        case "12":
            rusult = rusult+"-31";
            break;
	}
    return rusult;
}

/**
 *
 * @param dateFormat dateFormat  如：'yyyy-MM-dd HH:mm:ss'
 * @param type 时间类型：YEAR 、MONTH 、DATE
 * @param isAdd 是否做加或减法运算 0减/加 空不做运算
 * @param value 加减的值
 * @returns {string}
 */
function getSystemTime(dateFormat,type,isAdd,value){
    var systemTime="";
    $.ajax({
        url:basePath+"/system/getSystemTime",
        async: false,
        dataType: "json",
        type: "POST",
        contentType: "application/x-www-form-urlencoded",
        data:{
            dateFormat:dateFormat,
            type:type,
            isAdd:isAdd,
            value:value
        },
        beforeSend: null,
        success: function (data) {
            if(data.status==1){
                systemTime=data.data;
            }
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            $.wdfProgress.showMsg("请求服务器异常！",$("body"),null,null);
            $.wdfProgress.progRemove($("body"),false,1000);
        }
    });
    return systemTime;
}

function getLastMonthDate(){
    var nowdays = new Date();
    var year = nowdays.getFullYear();
    var month = nowdays.getMonth();
    if(month==0){
        month = 12;
        year = year-1;
    }
    if(month<10){
        month = '0'+month;
    }
    var myDate = new Date(year,month,0);
    return myDate;
}
function mGetDate(year, month){
    var d = new Date(year, month, 0);
    return d.getDate();
}

